﻿Public Class AIZeroReadForm

    Private _pi As ProjectItem

    '选择设备的网络地址
    Private _netID As Byte = 0

    'AI数据的起始地址
    Private _offset As UShort = 20000

    Private _offsetType As UShort = 12   '兼容模块探头类型值
    Private _offsetNum As UShort = 2010   '兼容模块数量起始地址
    Private ExAiNum As Integer   '兼容模块配置AI数量
    Private buf1() As UShort

    Private Sub AIValueForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _pi = MainForm.pi

        'ProjectService.RefreshCurProjectList(_pi.ProjectID) '刷新当前工程信息

        ClearData()

        Me.HideAllControl()

        LoadAllRef()
    End Sub


    Private Sub ClearData()
        For Each ctl As Control In Me.ReadPanel.Controls
            If ctl.GetType Is GetType(TextBox) Then
                CType(ctl, TextBox).Text = ""
            End If
        Next
    End Sub

    '隐藏/显示Panel中的所有控件
    Private Sub HideAllControl()
        For Each item As Control In Me.ReadPanel.Controls
            item.Visible = False
        Next

        Me.btnRead.Enabled = False
        Me.btnStop.Enabled = False
        Me.btnClear.Enabled = False
        Me.btnClose.Enabled = True
        Me.lbl_tip.Visible = True
    End Sub



    Private Sub LoadAllRef()
        Dim tn As TreeNode
        Dim dts As New DevTypeService(_pi)

        For Each dti As DevTypeItem In dts.GeDevGroupList()   '设备列表
            tn = Me.TreeView1.Nodes.Add(String.Format("{0}号设备", dti.NetId))
            tn.Tag = dti
        Next

        Me.TreeView1.ExpandAll()

    End Sub



    Private Sub SetPanelLayout(ByVal DevType As Integer)

        txtAI1.Tag = 0
        txtAI2.Tag = 1
        txtAI3.Tag = 2
        txtAI4.Tag = 3
        txtAI5.Tag = 4
        txtAI6.Tag = 5
        If DevType = 1 Then ' RI300设备
            txtAI7.Tag = 6
            txtAI8.Tag = 7
            txtAI9.Tag = 8
            txtAI10.Tag = 9
            txtAI11.Tag = 10
            txtAI12.Tag = 11
            Label7.Text = "AI 7:"
            Label8.Text = "AI 8:"
        ElseIf DevType = 2 Then 'RI300E
            txtAI7.Tag = 9
            txtAI8.Tag = 10
            txtAI9.Tag = 0
            txtAI10.Tag = 0
            txtAI11.Tag = 0
            txtAI12.Tag = 0
            Label7.Text = "AI10:"
            Label8.Text = "AI11:"
        End If

        txtAI1.Visible = True
        txtAI2.Visible = True
        txtAI3.Visible = True
        txtAI4.Visible = True
        txtAI5.Visible = True
        txtAI6.Visible = True
        txtAI7.Visible = True
        txtAI8.Visible = True

        txtAI13.Visible = False
        txtAI14.Visible = False
        txtAI15.Visible = False
        txtAI16.Visible = False

        If DevType = 1 Then ' RI300设备
            txtAI9.Visible = True
            txtAI10.Visible = True
            txtAI11.Visible = True
            txtAI12.Visible = True

        ElseIf DevType = 2 Then 'RI300E
            txtAI9.Visible = False
            txtAI10.Visible = False
            txtAI11.Visible = False
            txtAI12.Visible = False
        End If

        Label1.Visible = txtAI1.Visible
        Label2.Visible = txtAI2.Visible
        Label3.Visible = txtAI3.Visible
        Label4.Visible = txtAI4.Visible
        Label5.Visible = txtAI5.Visible
        Label6.Visible = txtAI6.Visible
        Label7.Visible = txtAI7.Visible
        Label8.Visible = txtAI8.Visible
        Label9.Visible = txtAI9.Visible
        Label10.Visible = txtAI10.Visible
        Label11.Visible = txtAI11.Visible
        Label12.Visible = txtAI12.Visible
        Label13.Visible = txtAI13.Visible
        Label14.Visible = txtAI14.Visible
        Label15.Visible = txtAI15.Visible
        Label16.Visible = txtAI16.Visible
    End Sub

    Private Sub SetPanelLayoutEx16(ByVal DevType As Integer)

        txtAI1.Tag = 0
        txtAI2.Tag = 1
        txtAI3.Tag = 2
        txtAI4.Tag = 3
        txtAI5.Tag = 4
        txtAI6.Tag = 5

        txtAI7.Tag = 6
        txtAI8.Tag = 7
        txtAI9.Tag = 8
        txtAI10.Tag = 9
        txtAI11.Tag = 10
        txtAI12.Tag = 11
        txtAI13.Tag = 12
        txtAI14.Tag = 13
        txtAI15.Tag = 14
        txtAI16.Tag = 15
        

        txtAI1.Visible = True
        txtAI2.Visible = True
        txtAI3.Visible = True
        txtAI4.Visible = True
        txtAI5.Visible = True
        txtAI6.Visible = True
        txtAI7.Visible = True
        txtAI8.Visible = True
        txtAI9.Visible = True
        txtAI10.Visible = True
        txtAI11.Visible = True
        txtAI12.Visible = True
        txtAI13.Visible = True
        txtAI14.Visible = True
        txtAI15.Visible = True
        txtAI16.Visible = True
        

      
        Label1.Visible = txtAI1.Visible
        Label2.Visible = txtAI2.Visible
        Label3.Visible = txtAI3.Visible
        Label4.Visible = txtAI4.Visible
        Label5.Visible = txtAI5.Visible
        Label6.Visible = txtAI6.Visible
        Label7.Visible = txtAI7.Visible
        Label8.Visible = txtAI8.Visible
        Label9.Visible = txtAI9.Visible
        Label10.Visible = txtAI10.Visible
        Label11.Visible = txtAI11.Visible
        Label12.Visible = txtAI12.Visible
        Label13.Visible = txtAI13.Visible
        Label14.Visible = txtAI14.Visible
        Label15.Visible = txtAI15.Visible
        Label16.Visible = txtAI16.Visible
    End Sub


    ''' <summary>
    ''' 刷新AI数据
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub RefreshData()
        Dim buf() As UShort
        buf = COMHelper.Instance.Send4UShort(_netID, FunctionCode.WordReadonly, _offset, 12)

        If buf Is Nothing Then
            'ClearData()
            'msg.ShowWarning("读数据失败")
            Exit Sub
        End If

        For Each ctl As Control In Me.ReadPanel.Controls
            If ctl.GetType Is GetType(TextBox) Then
                Dim nud As TextBox = CType(ctl, TextBox)
                If nud.Visible = True Then
                    If buf(nud.Tag) <> 0 Then
                        nud.Text = buf(nud.Tag)
                    End If

                End If
                
            End If
        Next

    End Sub
    ''' <summary>
    ''' 刷新AI数据
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub RefreshDataEx16()
        Dim buf() As Short = Nothing

        For i As Integer = 0 To ExAiNum - 1
            Try
                For k As Integer = 0 To 1
                    buf = COMHelper.Instance.Send4Short(_netID, FunctionCode.WordReadonly, _offset + i, 1)
                    If buf Is Nothing Then
                        Exit For
                    Else
                    End If

                Next

                If buf Is Nothing Then
                    Continue For
                End If


                txtList(i).Text = buf(0)
            Catch ex As Exception

            End Try

        Next

    End Sub

    ''' <summary>
    ''' 关闭窗体
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Timer1.Stop()
        Me.Close()
    End Sub

    ''' <summary>
    ''' 开始读
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        TreeView1.Enabled = False
        Me.Timer1.Start()
    End Sub

    ''' <summary>
    ''' 停止读
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        Me.Timer1.Stop()
        TreeView1.Enabled = True
    End Sub

    Private _isReading As Boolean = False

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If _isReading Then
            Exit Sub
        End If
        Application.DoEvents()

        _isReading = True

        Me.Cursor = Cursors.WaitCursor

        Dim devtype As Integer
        devtype = DevTypeService.GetdevType(MainForm.pi.ProjectID, _netID)

        Select Case devtype
            Case 1
                RefreshData()
            Case 16
                RefreshDataEx16()
        End Select

        Me.Cursor = Cursors.Default

        _isReading = False
    End Sub


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.ClearData()
    End Sub

    ''' <summary>
    ''' 关闭窗体
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        Me.Timer1.Stop()
        MyBase.OnClosing(e)
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim tn As TreeNode
        Dim dti As DevTypeItem
        Dim TimerStatus As Boolean = False

        tn = Me.TreeView1.SelectedNode

        If tn Is Nothing Then
            Exit Sub
        End If

        If TypeOf tn.Tag Is DevTypeItem Then




            TimerStatus = Timer1.Enabled
            Me.Timer1.Stop()

            ClearData()

            lbl_tip.Visible = False

            dti = CType(tn.Tag, DevTypeItem)

            '显示选择设备的名称
            Me.lblSelected.Text = tn.Text

            Me.btnRead.Enabled = True
            Me.btnStop.Enabled = True
            Me.btnClear.Enabled = True
            Me.btnClose.Enabled = True
            _netID = dti.NetId   '保存当前选择的设备号

            Select Case dti.DevAppType
                Case 1, 17
                    SetToolTip(dti.NetId)
                    SetPanelLayout(dti.DevType)

                Case 16, 18


                    'If ExType() Then
                    '    '读取兼容模块配置端口数量
                    '    ExAiNum = GetEcAivalue(dti.NetId)

                    '    If ExAiNum = 0 Then
                    '        msg.Show("未配置兼容模块探头数量！")

                    '    End If


                    'Else
                    '    msg.Show("该类型兼容模块不支持读取！")
                    '    Exit Sub

                    'End If

                    SetToolTipEx16(dti.NetId)
                    SetPanelLayoutEx16(dti.DevType)

            End Select




            If TimerStatus = True Then
                Me.Timer1.Start()
            End If


        End If

    End Sub
    ''' <summary>
    ''' 返回兼容模块中模拟传感器数量
    ''' </summary>
    ''' <param name="netid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetEcAivalue(ByVal netid As Integer) As Integer
        Dim k() As UShort = Nothing
        Try
            k = COMHelper.Instance.Send4UShort(_netID, FunctionCode.WordWritableRead, _offsetNum, 1)
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        Finally
            Me.Cursor = Cursors.Default

        End Try

        If k Is Nothing Then

            Return 0
        End If


        Return k(0)
    End Function
    ''' <summary>
    ''' 返回兼容模块类型0:未配置；1:TBCC传感器； 2：春长仪器；255:模拟传感器
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ExType() As Boolean

        Dim k As Integer
        Try
            buf1 = COMHelper.Instance.Send4UShort(_netID, FunctionCode.WordWritableRead, _offsetType, 1)
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        Finally
            Me.Cursor = Cursors.Default

        End Try

        If buf1 Is Nothing Then
           
            Return False
        End If

        k = buf1(0)


        If k = 1 Then
            Return True
        Else
            Return False
        End If

    End Function

    Private labList(15) As Label
    Private txtList(15) As TextBox
    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        labList(0) = Label1
        labList(1) = Label2
        labList(2) = Label3
        labList(3) = Label4
        labList(4) = Label5
        labList(5) = Label6
        labList(6) = Label7
        labList(7) = Label8
        labList(8) = Label9
        labList(9) = Label10
        labList(10) = Label11
        labList(11) = Label12
        labList(12) = Label13
        labList(13) = Label14
        labList(14) = Label15
        labList(15) = Label16
        For Each l As Label In labList
            ToolTip1.SetToolTip(l, "未使用")
        Next

        txtList(0) = txtAI1
        txtList(1) = txtAI2
        txtList(2) = txtAI3
        txtList(3) = txtAI4
        txtList(4) = txtAI5
        txtList(5) = txtAI6
        txtList(6) = txtAI7
        txtList(7) = txtAI8
        txtList(8) = txtAI9
        txtList(9) = txtAI10
        txtList(10) = txtAI11
        txtList(11) = txtAI12
        txtList(12) = txtAI13
        txtList(13) = txtAI14
        txtList(14) = txtAI15
        txtList(15) = txtAI16
        For Each n As TextBox In txtList
            ToolTip1.SetToolTip(n, "未使用")
        Next
    End Sub

    Private Sub SetToolTip(ByVal NetID As Integer)
        Dim portNo As Integer
        Dim str As String
        Dim portType As String = "未知"
        Dim rts As New RefTypeService(_pi)

        For i As Integer = 0 To 11
            ToolTip1.SetToolTip(labList(i), "未使用")
            ToolTip1.SetToolTip(txtList(i), "未使用")
        Next

        For Each rti As RefTypeItem In rts.GeRefGroupList()
            If rti.NetId = NetID Then
                Dim ats As New AiPortTypeService(rti)
                For Each ati As AiPortTypeItem In ats.GetAiGroupList()
                    portNo = ati.PortNo - 1
                    If portNo < 0 Then Continue For
                    If ati.DataType = 1 Then
                        portType = "温度"
                    ElseIf ati.DataType = 2 Then
                        portType = "湿度"
                    End If
                    str = String.Format("{0}: {1}({2})", rti.RefName, ati.PortName, portType)
                    ToolTip1.SetToolTip(labList(portNo), str)
                    ToolTip1.SetToolTip(txtList(portNo), str)
                Next
            End If
        Next

    End Sub

    Private Sub SetToolTipEx16(ByVal NetID As Integer)
        Dim portNo As Integer
        Dim str As String
        Dim portType As String = "未知"
        Dim rts As New RefTypeService(_pi)

        For i As Integer = 0 To 15
            ToolTip1.SetToolTip(labList(i), "未使用")
            ToolTip1.SetToolTip(txtList(i), "未使用")
        Next

        For Each rti As RefTypeItem In rts.GeRefGroupList()
            If rti.NetId = NetID Then
                Dim ats As New AiPortTypeService(rti)
                For Each ati As AiPortTypeItem In ats.GetAiGroupList()
                    portNo = ati.PortNo - 1
                    If portNo < 0 Then Continue For
                    If ati.DataType = 1 Then
                        portType = "温度"
                    ElseIf ati.DataType = 2 Then
                        Continue For
                        portType = "湿度"
                    End If
                    str = String.Format("{0}: {1}({2})", rti.RefName, ati.PortName, portType)
                    ToolTip1.SetToolTip(labList(portNo), str)
                    ToolTip1.SetToolTip(txtList(portNo), str)
                Next
            End If
        Next

    End Sub

End Class